import os,sys
import threading
import time
from PyQt5.QtWidgets import *
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import QSize, Qt, QCoreApplication
from PyQt5.QtGui import QIcon, QPixmap
from PyQt5.QtWidgets import QFileDialog, QListWidgetItem, QListView, QMessageBox, QDesktopWidget
from test import QMainWindow_new

l_photo = ['jpg', "JPG", "png", "PNG", "JPEG", "jpeg", "gif", "GIF", ".bmp"]  # 图片类型
l_photo_point = ['.jpg', ".JPG", ".png", ".PNG", ".JPEG", ".jpeg", ".gif", ".GIF", ".bmp"]  # 图片类型

class Ui_photoshower(QMainWindow_new):
    # 路径
    Path = ''
    # 播放间隔.单位:s
    Interval = 0.3
    # 当前照片计数
    Index = 0
    img = None
    scaled_img = None
    s_width_up = 0  # 记录变化
    s_heigh_up = 0
    s_width_down = 0
    s_heigh_down = 0
    scaled_img_width = 0  # 记录高宽
    scaled_img_height = 0
    count_all = 0  # 记录总数
    count = 0  # 记录已经读取了多少张照片
    flag_bar = False
    datasets_train = None
    datasets_val = None
    datasets_test = None

    def __init__(self):
        super(Ui_photoshower, self).__init__()
        self.setupUi(self)

    def setupUi(self, photoshower):
        font = QtGui.QFont()
        font.setPixelSize(26)
        font.setBold(True)
 # ————————————————————————————————————————————————————————————
        label_backdrop = QLabel(self)
        label_backdrop.resize(1500, 865)
        label_backdrop.setStyleSheet("background-color: rgb(240, 250, 250);")

        label_title = QLabel(self)
        label_title.resize(1500, 50)
        label_title.setText("    数据集查看")
        label_title.setFont(QtGui.QFont("Adobe 黑体 Std R", 16))
        label_title.setStyleSheet("background-color: rgb(50, 200, 200);")

        label_T = QLabel(self)
        label_T.resize(40, 40)
        label_T.move(5, 5)
        label_T.setPixmap(QPixmap(r"ui\label.jpg").scaled(label_T.size(), Qt.KeepAspectRatio, Qt.SmoothTransformation))

        button_close = QPushButton(self)
        button_close.setStyleSheet("QPushButton {border: none;}")
        button_close.setIcon(QIcon(r"ui\close.png"))
        button_close.resize(50, 50)
        button_close.move(1430, 0)
        button_close.clicked.connect(self.close)

        button_min = QPushButton(self)
        button_min.setStyleSheet("QPushButton {border: none;}")
        button_min.setIcon(QIcon(r"ui\min.png"))
        button_min.resize(50, 50)
        button_min.move(1380, 0)
        button_min.clicked.connect(self.showMinimized)
# ————————————————————————————————————————————————————————————
        photoshower.setObjectName("photoshower")
        photoshower.resize(1500, 865)
        photoshower.setMinimumSize(QtCore.QSize(1480, 865))
        photoshower.setMaximumSize(QtCore.QSize(1480, 865))
        self.p_page_groupBox = QtWidgets.QGroupBox(photoshower)
        self.p_page_groupBox.setGeometry(QtCore.QRect(0, 50, 1500, 815))
        self.p_page_groupBox.setObjectName("p_page_groupBox")

        self.p_fuction_groupBox = QtWidgets.QGroupBox(self.p_page_groupBox)
        self.p_fuction_groupBox.setMinimumSize(QtCore.QSize(200, 0))
        self.p_fuction_groupBox.setObjectName("p_fuction_groupBox")
# ————————————————————————————————————————————————————————————
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Preferred)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.p_fuction_groupBox.sizePolicy().hasHeightForWidth())
        self.p_fuction_groupBox.setSizePolicy(sizePolicy)
# ————————————————————————————————————————————————————————————
        self.layoutWidget = QtWidgets.QWidget(self.p_fuction_groupBox)
        self.layoutWidget.setGeometry(QtCore.QRect(1320, 30, 130, 720))
        self.layoutWidget.setObjectName("layoutWidget")

        self.verticalLayout = QtWidgets.QVBoxLayout(self.p_page_groupBox)
        self.verticalLayout.setContentsMargins(0, 0, 0, 0)
        self.verticalLayout.setObjectName("verticalLayout")

        self.verticalLayout_2 = QtWidgets.QVBoxLayout(self.layoutWidget)
        self.verticalLayout_2.setContentsMargins(0, 0, 0, 0)
        self.verticalLayout_2.setObjectName("verticalLayout_2")
# ————————————————————————————————————————————————————————————
        self.p_horizontalLayout_1 = QtWidgets.QHBoxLayout()
        self.p_horizontalLayout_1.setObjectName("p_horizontalLayout_1")

        self.p_horizontalLayout_2 = QtWidgets.QHBoxLayout()
        self.p_horizontalLayout_2.setSpacing(0)
        self.p_horizontalLayout_2.setObjectName("p_horizontalLayout_2")
# ————————————————————————————————————————————————————————————
        self.p_choosing_photo_label = QtWidgets.QLabel(self.p_fuction_groupBox)
        self.p_choosing_photo_label.setGeometry(QtCore.QRect(10, 750, 200, 30))
        self.p_choosing_photo_label.setFont(font)
        self.p_choosing_photo_label.setObjectName("p_choosing_photo_label")
# ————————————————————————————————————————————————————————————
        self.p_photo_list_listwidget = QtWidgets.QListWidget(self.p_fuction_groupBox)
        self.p_photo_list_listwidget.setGeometry(QtCore.QRect(10, 60, 261, 651))
        self.p_photo_list_listwidget.setObjectName("p_photo_list_listwidget")
# ————————————————————————————————————————————————————————————
        self.p_groupbox = QtWidgets.QGroupBox(self.p_fuction_groupBox)
        self.p_groupbox.setGeometry(QtCore.QRect(280, 50, 1031, 661))
        self.p_groupbox.setFont(font)
        self.p_groupbox.setAlignment(QtCore.Qt.AlignCenter)
        self.p_groupbox.setObjectName("p_groupbox")
# ————————————————————————————————————————————————————————————
        self.p_show_photo_lable = QtWidgets.QLabel(self.p_groupbox)
        self.p_show_photo_lable.setStyleSheet("background-color: rgb(0, 0, 0);")
        self.p_show_photo_lable.setGeometry(QtCore.QRect(-90, 40, 1201, 601))
        self.p_show_photo_lable.setObjectName("p_show_photo_lable")
# ————————————————————————————————————————————————————————————
        self.p_look_train_button = QtWidgets.QPushButton(self.layoutWidget)
        self.p_look_train_button.setFont(font)
        self.p_look_train_button.setObjectName("p_opening_photofile_button")
        self.verticalLayout_2.addWidget(self.p_look_train_button)
# ————————————————————————————————————————————————————————————
        self.p_look_val_button = QtWidgets.QPushButton(self.layoutWidget)
        self.p_look_val_button.setFont(font)
        self.p_look_val_button.setObjectName("p_opening_photo_button")
        self.verticalLayout_2.addWidget(self.p_look_val_button)
# ————————————————————————————————————————————————————————————
        self.p_look_test_button = QtWidgets.QPushButton(self.layoutWidget)
        self.p_look_test_button.setFont(font)
        self.p_look_test_button.setObjectName("p_opening_photo_button")
        self.verticalLayout_2.addWidget(self.p_look_test_button)
# ————————————————————————————————————————————————————————————
        self.p_upphoto_button = QtWidgets.QPushButton(self.layoutWidget)
        self.p_upphoto_button.setFont(font)
        self.p_upphoto_button.setObjectName("p_upphoto_button")
        self.verticalLayout_2.addWidget(self.p_upphoto_button)
# ————————————————————————————————————————————————————————————
        self.p_downphoto_button = QtWidgets.QPushButton(self.layoutWidget)
        self.p_downphoto_button.setFont(font)
        self.p_downphoto_button.setObjectName("p_downphoto_button")
        self.verticalLayout_2.addWidget(self.p_downphoto_button)
# ————————————————————————————————————————————————————————————
        self.p_auto_show_button = QtWidgets.QPushButton(self.layoutWidget)
        self.p_auto_show_button.setFont(font)
        self.p_auto_show_button.setObjectName("p_auto_show_button")
        self.verticalLayout_2.addWidget(self.p_auto_show_button)
# ————————————————————————————————————————————————————————————
        self.p_end_auto_show_button = QtWidgets.QPushButton(self.layoutWidget)
        self.p_end_auto_show_button.setFont(font)
        self.p_end_auto_show_button.setObjectName("p_end_auto_show_button")
        self.verticalLayout_2.addWidget(self.p_end_auto_show_button)
# ————————————————————————————————————————————————————————————
        self.p_little_button = QtWidgets.QPushButton(self.layoutWidget)
        self.p_little_button.setFont(font)
        self.p_little_button.setObjectName("p_little_button")
        self.verticalLayout_2.addWidget(self.p_little_button)
# ————————————————————————————————————————————————————————————
        self.p_big_button = QtWidgets.QPushButton(self.layoutWidget)
        self.p_big_button.setFont(font)
        self.p_big_button.setObjectName("p_big_button")
        self.verticalLayout_2.addWidget(self.p_big_button)
# ————————————————————————————————————————————————————————————
        self.p_photo_rode_label = QtWidgets.QLabel(self.p_fuction_groupBox)
        self.p_photo_rode_label.setGeometry(QtCore.QRect(180, 750, 650, 30))       # 路径
        self.p_photo_rode_label.setFont(font)
        self.p_photo_rode_label.setText("图片路径")
        self.p_photo_rode_label.setObjectName("p_photo_rode_label")
# ————————————————————————————————————————————————————————————
        self.p_show_photo_name_label = QtWidgets.QLabel(self.p_fuction_groupBox)
        self.p_show_photo_name_label.setAlignment(Qt.AlignCenter)
        self.p_show_photo_name_label.setGeometry(QtCore.QRect(647, 710, 300, 30))
        self.p_show_photo_name_label.setFont(font)
        self.p_show_photo_name_label.setObjectName("p_show_photo_name_label")
# ————————————————————————————————————————————————————————————
        self.p_bar_label = QtWidgets.QLabel(self.p_fuction_groupBox)
        self.p_bar_label.setGeometry(QtCore.QRect(900, 750, 250, 30))
        self.p_bar_label.setFont(font)
        self.p_bar_label.setObjectName("p_bar_label")
# ————————————————————————————————————————————————————————————
        self.p_read_progressBar = QtWidgets.QProgressBar(self.p_fuction_groupBox)
        self.p_read_progressBar.setGeometry(QtCore.QRect(1100, 750, 360, 30))
        self.p_read_progressBar.setProperty("value", 0)
        self.p_read_progressBar.setObjectName("p_read_progressBar")
        self.p_horizontalLayout_1.addWidget(self.p_fuction_groupBox)
        self.verticalLayout.addLayout(self.p_horizontalLayout_1)

        self.con()
        self.retranslateUi(photoshower)
        QtCore.QMetaObject.connectSlotsByName(photoshower)

    def retranslateUi(self, photoshower):
        _translate = QtCore.QCoreApplication.translate
        photoshower.setWindowTitle(_translate("photoshower", "查看数据集"))
        self.p_choosing_photo_label.setText(_translate("photoshower", "选择的图片集："))
        self.p_groupbox.setTitle(_translate("photoshower", "图片显示"))

        self.p_look_train_button.setText(_translate("photoshower", "训 练 集"))
        self.p_look_val_button.setText(_translate("photoshower", "验 证 集"))
        self.p_look_test_button.setText(_translate("photoshower", "测 试 集"))

        self.p_upphoto_button.setText(_translate("photoshower", "上 一 张"))
        self.p_downphoto_button.setText(_translate("photoshower", "下 一 张"))
        self.p_auto_show_button.setText(_translate("photoshower", "自动播放"))
        self.p_end_auto_show_button.setText(_translate("photoshower", "退出播放"))
        self.p_little_button.setText(_translate("photoshower", "缩小图片"))
        self.p_big_button.setText(_translate("photoshower", "放大图片"))
        self.p_show_photo_name_label.setText(_translate("photoshower", "图片名称"))
        self.p_bar_label.setText(_translate("photoshower", "图片集读取进度："))
        # self.p_photo_rode_label.setAlignment(Qt.AlignCenter)

        # 初始化禁用组件，避免误操作
        self.p_upphoto_button.setDisabled(True)
        self.p_downphoto_button.setDisabled(True)
        self.p_auto_show_button.setDisabled(True)
        self.p_end_auto_show_button.setDisabled(True)
        self.p_big_button.setDisabled(True)
        self.p_little_button.setDisabled(True)

    # 解锁
    def unlock(self):
        self.p_upphoto_button.setDisabled(False)
        self.p_downphoto_button.setDisabled(False)
        self.p_auto_show_button.setDisabled(False)
        self.p_big_button.setDisabled(False)
        self.p_little_button.setDisabled(False)

    # 图片名字
    def show_photo_name(self):
        font = QtGui.QFont()
        font.setFamily("Adobe 黑体 Std R")
        font.setPixelSize(21)
        font.setBold(True)
        self.p_show_photo_name_label.setFont(font)
        self.p_show_photo_name_label.setAlignment(Qt.AlignCenter)
        self.p_show_photo_name_label.setText(self.img_paths_short[self.currentImgIdx])

    def get_all_photo(self): #获得总数
        for root, dirs, files in os.walk(self.Path):
            for file in files:
                ext = os.path.splitext(file)[-1].lower()
                if ext in l_photo_point:
                    self.count_all += 1
        return self.count_all

    def photo_read_bar(self): #进度条
        file_list = os.listdir(self.Path)
        self.count_all = 0
        self.count_all = self.get_all_photo()
        self.p_read_progressBar.setMaximum(self.count_all)
        self.flag_bar = True
        self.count = 0
        while self.flag_bar:
            # 使用 with 语句创建一个进度条
            for img_path in self.img_paths:
                if os.path.isfile(img_path):
                    img_name = os.path.basename(img_path)
                    item = QListWidgetItem(QIcon(img_path), img_name)
                    self.p_photo_list_listwidget.setFlow(QListView.Flow(1))  # 0: left to right,1: top to bottom
                    self.p_photo_list_listwidget.setIconSize(QSize(150, 100))
                    self.p_photo_list_listwidget.addItem(item)
                    self.count += 1
                    self.p_read_progressBar.setValue(self.count)  # qt的进度条
            if self.count == self.count_all: #写入完成
                break
            break


    def get_zoom_scale(self, image_height, image_width, label_height, label_width):  # 获得比例
        zoom_scale = 1
        width_pus = image_width / label_width
        height_pus = image_height / label_height
        zoom_scale = max(width_pus, height_pus)
        return zoom_scale

    # 打开文件夹
    def imgs_position(self,dir=None):
        self.p_photo_list_listwidget.clear()
        self.p_show_photo_lable.clear()
        self.p_show_photo_name_label.clear()
        self.p_upphoto_button.setDisabled(True)
        self.p_downphoto_button.setDisabled(True)
        self.p_auto_show_button.setDisabled(True)
        self.p_big_button.setDisabled(True)
        self.p_little_button.setDisabled(True)
        if dir=="train":
            directory = self.datasets_train
        elif dir=="val":
            directory = self.datasets_val
        elif dir=="test":
            directory = self.datasets_test
        else:
            directory = QFileDialog.getExistingDirectory(self, "选取文件夹", "./")  # 起始路径
        try:
            if directory != None:
                self.p_photo_list_listwidget.clear()
                self.p_show_photo_name_label.clear()
                self.flag_photo = False
                font = QtGui.QFont()
                font.setFamily("Adobe 黑体 Std R")
                font.setPixelSize(21)
                font.setBold(True)
                self.p_photo_rode_label.setFont(font)
                self.p_photo_rode_label.setText(str(directory))
                filenames = os.listdir(directory)
                self.Path = os.path.join(directory)

                self.img_paths = []
                self.img_paths_short = []  # 显示第几张图片
                for file in filenames:
                    if file[-3:] in l_photo:
                        self.img_paths.append(os.path.join(directory, file))
                        self.img_paths_short.append(file)

                if len(self.img_paths) == 0:
                    self.p_show_photo_name_label.clear()
                    self.p_photo_rode_label.clear()
                    QMessageBox.warning(self, "警告！", "读取的文件夹无所展示的图片类型")

                # 显示设置listWidget
                self.photo_read_bar() #进度条展示
                self.p_photo_list_listwidget.setCurrentRow(0)  # 初始化图片位置

            else:
                QMessageBox.warning(self, "警告！", "读取的文件夹不存在或未选择文件夹路径")
                self.p_photo_rode_label.clear()

        except TypeError:
            QMessageBox.warning(self, "警告！", "信息读取失败")
            self.p_photo_rode_label.clear()

    # 加载文件夹里面的图片
    def loadImage(self):
        self.unlock()  # 解锁
        self.currentImgIdx = self.p_photo_list_listwidget.currentIndex().row()
        if self.currentImgIdx in range(len(self.img_paths)):
            # self.show_photo.setScaledContents(False)  # 防止失真
            self.img = QPixmap(self.img_paths[self.currentImgIdx])
            self.scaled_img = self.img

            if self.scaled_img:
                zoom_scale = self.get_zoom_scale(self.scaled_img.height(), self.scaled_img.width(),self.p_show_photo_lable.height(), self.p_show_photo_lable.width())
                if self.scaled_img.width() <= self.p_show_photo_lable.width() and self.scaled_img.height() <= self.p_show_photo_lable.height():
                    currentImg = QtGui.QPixmap(self.img_paths[self.currentImgIdx]).scaled(self.scaled_img.width(),self.scaled_img.height())
                else:
                    currentImg = QtGui.QPixmap(self.img_paths[self.currentImgIdx]).scaled(
                        self.scaled_img.width() // zoom_scale,
                        self.scaled_img.height() // zoom_scale)
                self.p_show_photo_lable.setPixmap(currentImg)
                self.p_show_photo_lable.setAlignment(Qt.AlignCenter)
                self.show_photo_name()
    # 查看上一张
    def lastimg(self):
        self.p_end_auto_show_button.setDisabled(True)
        self.s_width = 0
        self.s_heigh = 0
        self.currentImgIdx = self.currentImgIdx - 1
        if self.currentImgIdx < 0:  # 查看上一张时不循环查看
            self.currentImgIdx = self.currentImgIdx + 1
            QMessageBox.warning(self, "警告！", "无上一张图片")
        else:
            self.p_photo_list_listwidget.setCurrentRow(self.currentImgIdx)
            if self.currentImgIdx in range(len(self.img_paths)):
                self.p_show_photo_lable.setScaledContents(False)
                self.img = QPixmap(self.img_paths[self.currentImgIdx])
                self.scaled_img = self.img
                if self.scaled_img:
                    zoom_scale = self.get_zoom_scale(self.scaled_img.height(), self.scaled_img.width(),self.p_show_photo_lable.height(), self.p_show_photo_lable.width())
                    if self.scaled_img.width() <= self.p_show_photo_lable.width() and self.scaled_img.height() <= self.p_show_photo_lable.height():
                        currentImg = QtGui.QPixmap(self.img_paths[self.currentImgIdx]).scaled(self.scaled_img.width(),self.scaled_img.height())
                    else:
                        currentImg = QtGui.QPixmap(self.img_paths[self.currentImgIdx]).scaled(self.scaled_img.width() // zoom_scale,self.scaled_img.height() // zoom_scale)
                    self.p_show_photo_lable.setPixmap(currentImg)
                    self.p_show_photo_lable.setAlignment(Qt.AlignCenter)
                    self.show_photo_name()

    # 查看下一张
    def nextimg(self):
        self.s_width = 0
        self.s_heigh = 0
        self.currentImgIdx = self.currentImgIdx + 1
        if self.currentImgIdx > len(self.img_paths) - 1:
            self.currentImgIdx = self.currentImgIdx - 1
            QMessageBox.warning(self, "警告！", "无下一张图片")
        else:
            self.p_photo_list_listwidget.setCurrentRow(self.currentImgIdx)  # 设置listWidget
            if self.currentImgIdx in range(len(self.img_paths)):
                self.p_show_photo_lable.setScaledContents(False)
                self.img = QPixmap(self.img_paths[self.currentImgIdx])
                self.scaled_img = self.img
                if self.scaled_img:
                    zoom_scale = self.get_zoom_scale(self.scaled_img.height(), self.scaled_img.width(),self.p_show_photo_lable.height(), self.p_show_photo_lable.width())
                    if self.scaled_img.width() <= self.p_show_photo_lable.width() and self.scaled_img.height() <= self.p_show_photo_lable.height():
                        currentImg = QtGui.QPixmap(self.img_paths[self.currentImgIdx]).scaled(self.scaled_img.width(),self.scaled_img.height())
                    else:
                        currentImg = QtGui.QPixmap(self.img_paths[self.currentImgIdx]).scaled(
                            self.scaled_img.width() // zoom_scale,
                            self.scaled_img.height() // zoom_scale)
                    self.p_show_photo_lable.setPixmap(currentImg)
                    self.p_show_photo_lable.setAlignment(Qt.AlignCenter)
                    self.show_photo_name()
                    return True
        return False

    # 放大
    def amplify(self):
        self.p_show_photo_lable.setScaledContents(False)
        zoom_scale = self.get_zoom_scale(self.scaled_img.height(), self.scaled_img.width(),self.p_show_photo_lable.height(), self.p_show_photo_lable.width())
        if self.scaled_img.width() <= self.p_show_photo_lable.width() and self.scaled_img.height() <= self.p_show_photo_lable.height():
            self.scaled_img_width = self.scaled_img.width() + self.s_width_up
            self.scaled_img_height = self.scaled_img.height() + self.s_heigh_up
            if not self.flag_photo:
                currentImg = QtGui.QPixmap(self.img_paths[self.currentImgIdx]).scaled(self.scaled_img_width,self.scaled_img_height)
            else:
                currentImg = QtGui.QPixmap(self.img_paths[0]).scaled(self.scaled_img_width,self.scaled_img_height)
            self.s_width_up += 10
            self.s_heigh_up += 10
        else:
            self.scaled_img_width = self.scaled_img.width() // zoom_scale + self.s_width_up
            self.scaled_img_height = self.scaled_img.height() // zoom_scale + self.s_heigh_up
            if not self.flag_photo:
                currentImg = QtGui.QPixmap(self.img_paths[self.currentImgIdx]).scaled(self.scaled_img_width,
                                                                                      self.scaled_img_height)
            else:
                currentImg = QtGui.QPixmap(self.img_paths[0]).scaled(self.scaled_img_width,self.scaled_img_height)
            if self.scaled_img.width() > self.scaled_img.height():
                self.s_width_up += 10
                self.s_heigh_up += 3
            else:
                self.s_width_up += 3
                self.s_heigh_up += 10
        self.p_show_photo_lable.setPixmap(currentImg)
        self.p_show_photo_lable.setAlignment(Qt.AlignCenter)
        self.s_width_down = 0
        self.s_heigh_down = 0

    # 缩小
    def shrink(self):
        self.p_show_photo_lable.setScaledContents(False)
        zoom_scale = self.get_zoom_scale(self.scaled_img.height(), self.scaled_img.width(),self.p_show_photo_lable.height(), self.p_show_photo_lable.width())
        if self.scaled_img.width() <= self.p_show_photo_lable.width() and self.scaled_img.height() <= self.p_show_photo_lable.height():
            self.scaled_img_width = self.scaled_img.width() - self.s_width_down
            self.scaled_img_height = self.scaled_img.height() - self.s_heigh_down
            if not self.flag_photo:
                currentImg = QtGui.QPixmap(self.img_paths[self.currentImgIdx]).scaled(self.scaled_img_width,self.scaled_img_height)
            else:
                currentImg = QtGui.QPixmap(self.img_paths[0]).scaled(self.scaled_img_width,self.scaled_img_height)
            self.s_width_down += 10
            self.s_heigh_down += 10
        else:
            self.scaled_img_width = self.scaled_img.width() // zoom_scale - self.s_width_down
            self.scaled_img_height = self.scaled_img.height() // zoom_scale - self.s_heigh_down
            if not self.flag_photo:
                currentImg = QtGui.QPixmap(self.img_paths[self.currentImgIdx]).scaled(self.scaled_img_width,self.scaled_img_height)
            else:
                currentImg = QtGui.QPixmap(self.img_paths[0]).scaled(self.scaled_img_width,self.scaled_img_height)
            if self.scaled_img.width() > self.scaled_img.height():
                self.s_width_down += 10
                self.s_heigh_down += 3
            else:
                self.s_width_down += 3
                self.s_heigh_down += 10
        self.p_show_photo_lable.setPixmap(currentImg)
        self.p_show_photo_lable.setAlignment(Qt.AlignCenter)
        self.s_width_up = 0
        self.s_heigh_up = 0
    # 自动切换图片
    def new_image_change(self):
        while self.flag:
            if self.currentImgIdx + 1 < len(self.img_paths):
                self.nextimg()
            else:
                self.end_img()
            time.sleep(self.Interval)

    # 开始自动切换图片
    def start_img(self):
        self.p_look_train_button.setDisabled(True)
        self.p_look_val_button.setDisabled(True)
        self.p_look_test_button.setDisabled(True)

        self.p_upphoto_button.setDisabled(True)
        self.p_downphoto_button.setDisabled(True)

        self.p_auto_show_button.setDisabled(True)
        self.p_end_auto_show_button.setDisabled(False)  # 解锁结束播放按钮

        self.p_big_button.setDisabled(True)
        self.p_little_button.setDisabled(True)

        self.flag = True
        threading.Thread(target=self.new_image_change).start()

    # 结束自动切换图片
    def end_img(self):
        self.flag = False
        self.currentImgIdx = self.p_photo_list_listwidget.currentIndex().row()

        self.p_look_train_button.setDisabled(False)
        self.p_look_val_button.setDisabled(False)
        self.p_look_test_button.setDisabled(False)

        self.p_upphoto_button.setDisabled(False)
        self.p_downphoto_button.setDisabled(False)

        self.p_auto_show_button.setDisabled(False)

        self.p_big_button.setDisabled(False)
        self.p_little_button.setDisabled(False)
        self.p_end_auto_show_button.setDisabled(True)  # 禁用结束播放按钮

    def con(self):
        self.p_photo_list_listwidget.itemSelectionChanged.connect(self.loadImage)

        self.p_look_train_button.clicked.connect(lambda :self.imgs_position(dir="train"))
        self.p_look_val_button.clicked.connect(lambda :self.imgs_position(dir="val"))
        self.p_look_test_button.clicked.connect(lambda :self.imgs_position(dir="test"))

        self.p_upphoto_button.clicked.connect(self.lastimg)
        self.p_downphoto_button.clicked.connect(self.nextimg)

        self.p_auto_show_button.clicked.connect(self.start_img)
        self.p_end_auto_show_button.clicked.connect(self.end_img)

        self.p_big_button.clicked.connect(self.amplify)
        self.p_little_button.clicked.connect(self.shrink)

    def setCenter(self):
        screen = QDesktopWidget().screenGeometry()
        size = self.geometry()
        self.move(int((screen.width() - size.width()) / 2), int((screen.height() - size.height()) / 2))

if __name__ =="__main__":
    QCoreApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling)
    app = QApplication([])
    s = Ui_photoshower()
    s.show()
    app.exec_()
    sys.exit()